package com.sphd.emanu.activity.topupservice;

import com.google.gson.JsonObject;
import com.sphd.emanu.mvp.BasePresenterImpl;
import com.sphd.emanu.pojo.market.Deposit;
import com.sphd.emanu.utils.GsonUtil;
import com.wzutils.L;

import java.math.BigDecimal;
import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * MVPPlugin
 *  邮箱 784787081@qq.com
 */

public class TopupservicePresenter extends BasePresenterImpl<TopupserviceContract.View> implements TopupserviceContract.Presenter{

    @Override
    public void selectDeposit(int org) {
        emanuService.selectDeposit(org).enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
//                L.e(response.toString());
                List<Deposit> depositList = GsonUtil.jsonToList(response.body(),Deposit.class);
                mView.setDeposList(depositList);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });
    }

    @Override
    public void updateIsDeposit(Integer configId, Boolean isDeposit) {
        emanuService.updateIsDeposit(configId, isDeposit).enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                mView.upIsRes(res(response));
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                    Error(t);
            }
        });
    }

    @Override
    public void updateDeposit(int id, int org, Boolean enable, BigDecimal threshold, BigDecimal amount) {
            emanuService.updateDeposit(id, org, enable, threshold, amount).enqueue(new Callback<JsonObject>() {
                @Override
                public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                    mView.upRes(res(response));
                }

                @Override
                public void onFailure(Call<JsonObject> call, Throwable t) {
                    Error(t);
                }
            });
    }

    @Override
    public void addDeposit(int org, Boolean enable, BigDecimal threshold, BigDecimal amount) {
            emanuService.addDeposit(org, enable, threshold, amount).enqueue(new Callback<JsonObject>() {
                @Override
                public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                    L.e(response.toString());
                    mView.recRes(res(response));
                }

                @Override
                public void onFailure(Call<JsonObject> call, Throwable t) {
                    Error(t);
                }
            });
    }

    @Override
    public void deleteDeposit(final int id, final Integer postion, final SweetAlertDialog sweetAlertDialog) {
        emanuService.deleteDeposit(id).enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                mView.delRes(res(response),postion,sweetAlertDialog);
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Error(t);
            }
        });
    }
}
